<?php

namespace plugin\enterprise\app\support\Cores;

use plugin\enterprise\app\Enterprise;
use plugin\enterprise\app\service\EnterpriseMenuService;

class Menu extends \plugin\owladmin\app\support\Cores\Menu
{
    public function userMenus()
    {
        if (!Enterprise::config('enterprise.auth.enable')) {
            return collect([]);
        }
        $user = Enterprise::user();
        if ($user->isAdministrator() || Enterprise::config('enterprise.auth.permission') === false) {
            $list = EnterpriseMenuService::make()->query()->orderBy('order')->get();
        } else {
            $user->load('roles.permissions.menus');
            $list = $user->roles
                ->pluck('permissions')
                ->flatten()
                ->pluck('menus')
                ->flatten()
                ->unique('id')
                ->sortBy('order');
        }
        return $list;
    }


    public function extra(): array
    {
        $extraMenus = [];

        if (Enterprise::config('enterprise.auth.enable')) {
            $extraMenus[] = [
                'name'      => 'user_setting',
                'path'      => '/user_setting',
                'component' => 'amis',
                'meta'      => [
                    'hide'         => true,
                    'title'        => admin_trans('admin.user_setting'),
                    'icon'         => 'material-symbols:manage-accounts',
                    'singleLayout' => 'basic',
                ],
            ];
        }

        if (Enterprise::config('enterprise.show_development_tools')) {
            $extraMenus = array_merge($extraMenus, $this->devToolMenus());
        }

        return $extraMenus;
    }
}